package com.example.demo.practice;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class UserManager extends Activity {
    private ImageView imageView;
    private ListView listView;
    private String user = "";
    private List<info> mlistInfo = new ArrayList();

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.usermanager);

        Intent intent = getIntent();
        user = intent.getStringExtra("user");

        listView = findViewById(R.id.user_manager);
        setInfo();  //给信息赋值函数，用来测试
        listView.setAdapter(new  ListViewAdapter(mlistInfo));
        //处理Item的点击事件
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                info getObject = mlistInfo.get(position);   //通过position获取所点击的对象
                String infoId = getObject.getId(); //获取信息id
                String infoTitle = getObject.getTitle();    //获取信息标题

                //Toast显示测试
                Toast.makeText(UserManager.this, "信息ID:" + infoId, Toast.LENGTH_SHORT).show();
            }
        });
    }

    public class imageviewListener implements View.OnClickListener {
        public void onClick(View v) {
            finish();
        }
    }

    public class ListViewAdapter extends BaseAdapter {
        View[] itemViews;

        public ListViewAdapter(List<info> mlistInfo) {
            // TODO Auto-generated constructor stub
            itemViews = new View[mlistInfo.size()];
            for(int i=0;i<mlistInfo.size();i++){
                info getInfo=mlistInfo.get(i);    //获取第i个对象
                //调用makeItemView，实例化一个Item
                itemViews[i]=makeItemView(
                        getInfo.getTitle(), getInfo.getText(),getInfo.getAvatar()
                );
            }
        }

        public int getCount() {
            return itemViews.length;
        }

        public View getItem(int position) {
            return itemViews[position];
        }

        public long getItemId(int position) {
            return position;
        }

        //绘制Item的函数
        private View makeItemView(String strTitle, String strText,int resId) {
            LayoutInflater inflater = (LayoutInflater) UserManager.this
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            // 使用View的对象itemView与R.layout.item关联
            View itemView = inflater.inflate(R.layout.manager_item, null);

            // 通过findViewById()方法实例R.layout.item内各组件
            TextView title = itemView.findViewById(R.id.title);
            title.setText(strTitle);    //填入相应的值
            TextView text = itemView.findViewById(R.id.text);
            text.setText(strText);
            ImageView imageView = itemView.findViewById(R.id.update);
            imageView.setImageResource(resId);

            return itemView;
        }


        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null)
                return itemViews[position];
            return convertView;
        }
    }

    public void setInfo(){
        mlistInfo.clear();
        int i=0;
        ArrayList<String> item = new ArrayList();
        item.add("用户头像：");
        item.add("用户名：");
        item.add("用户类型：");
        item.add("姓名：");
        item.add("性别：");
        item.add("年龄：");
        item.add("公司：");
        item.add("联系方式：");
        item.add("密码管理");
        ArrayList<String> text  = new ArrayList();
        text.add("");
        text.add(user);
        text.add("企业管理员");
        text.add("李一");
        text.add("男");
        text.add("37");
        text.add("123有限公司");
        text.add("10000000000");
        text.add("");

        while(i<9){
            info information = new info();
            information.setId("menu"+i);
            information.setTitle(item.get(i));
            information.setText(text.get(i));
            if(i == 0){
                information.setAvatar(R.drawable.user);
            }

            mlistInfo.add(information); //将新的info对象加入到信息列表中
            i++;
        }

    }
}
